/**
 * 客户联系人的编辑和添加
 */
//客户联系人id
var contractid = 0;

$(function() {
	//按钮
	$("button").button();
	window.contractid = $("#id").val();
});

var customeragent = {
	
	add : function(id,addArg){
		zendform.zendFormCreat(e,$.extend(true,{
			"url" : BASEURL + "/custagent/add/customerid/"+id
			,"dialog" : {
				width:505,
				height:300
			}
			,"afterRenderCreatForm" : function(){
				zendform.uploadTag($("#photo"));//设置swfupload附着的element
				appendSWFupload({
					"button_placeholder_id":"uploadIco"//设置swfupload按钮附着的element，它来自上一条语句
					,"upload_start_handler":function(){
						//开始上传
						zendform.uploadingTag($("#photo"));
					}
					,"upload_error_handler":function(file, errorCode, message){
						//上传出错时
						alert("上传错误："+message);
						$("#photo").next(".uploading").remove();
					}
				},{
					"ju_element":"photo" //通知上传的额外参数，这里的ju_element必须以ju开头，是返回值的复制element
				});
			}
		},addArg));
	}
	,edit : function(id,editArg){
		if(activeAttachItems.editArg){
			if(activeAttachItems.editArg.url){
				activeAttachItems.editArg.url = activeAttachItems.editArg.url.replace("{id}", id);
			}
		}
		zendform.zendFormCreat(e,$.extend(true,{
			"url":BASEURL + "/contractattachment/edit/id/{id}"
			,"afterRenderCreatForm":function(){
				zendform.uploadTag($("#photo"));//设置swfupload附着的element
				appendSWFupload({
					"button_placeholder_id":"uploadIco"//设置swfupload按钮附着的element，它来自上一条语句
					,"upload_start_handler":function(){
						//开始上传
						zendform.uploadingTag($("#photo"));
					}
					,"upload_error_handler":function(file, errorCode, message){
						//上传出错时
						alert("上传错误："+message);
						$("#photo").next(".uploading").remove();
					}
				},{
					"ju_element":"photo" //通知上传的额外参数，这里的ju_element必须以ju开头，是返回值的复制element
				});
			}
			,dialog:{
				width:505,
				height:300
			}
		},editArg));
	}
	,del : function(ids){
		var idsData = [];
		$.each(ids,function(i,n){
			var uid = n.id.toString().replace("row","") || "0";
			uid = uid;
			idsData.push(uid);
		});
		if(confirm("您确定要删除这"+ids.length+"项数据吗？")){
			$.ajax({
				"url":activeAttachItems.delurl,
				"data":{id:idsData},
				"type":"POST",
				"success":function(data){
					if(data.status > 0){
						var randId = "id_" + Date.parse(new Date());
			        	window.grids.flexReload();
			        	$("<div id='"+randId+"'>"+data.msg+"</div>").dialog({title:"SUCCESS!",height: 140,modal: true});
			        	window.setTimeout(function(){$("#"+randId).dialog("close");},1200);
			        }else{
				        $("<div>ERROR!"+data.msg+"</div>").dialog({title:"ERROR!",height: 140,modal: true});
			        }
				},
				"error":function(){
			    	$("<div>ERROR!<br>服务错误！可能是网络忙！</div>").dialog({title:"ERROR!",height: 140,modal: true});
				}
			});
		}
	}
	
	//上传域
	,appendSWFupload : function(swfupArg,userArg){
		swfu = new SWFUpload($.extend({
			// Backend Settings
			upload_url: BASEURL + "/file/upload",
			post_params: $.extend({"PHPSESSID": PHPSESSID,"ju_element":"xxx"},userArg),
	
			// File Upload Settings
			file_size_limit : "2 MB",	// 2MB
			file_types : "*.*",
			file_types_description : "Every File You Want",
			file_upload_limit : "0",
	
			//upload_start_handler : fileUpSatrt,
			file_queue_error_handler : fileQueueError,
			file_dialog_complete_handler : fileDialogComplete,
			upload_progress_handler : uploadProgress,
			upload_error_handler : uploadError,
			upload_success_handler : uploadSuccess,
			upload_complete_handler : uploadComplete,
	
			// Button Settings
			button_image_url : BASEURL + "/js/swfupload/SmallSpyGlassWithTransperancy_17x18.png",
			button_placeholder_id : "",
			button_width: 18,
			button_height: 18,
			button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
			button_cursor: SWFUpload.CURSOR.HAND,
			
			// Flash Settings
			flash_url : BASEURL + "/js/swfupload/swfupload.swf",
	
			custom_settings : {
				upload_target : "zend_up_uploading"
			},
			
			// Debug Settings
			debug: false
		},swfupArg));
	
		
		/*$("#sltdiv").insertAfter("#mag_thumbnail");
		$("#mag_thumbnail").css("float","left");*/
	}
	//上传成功共
	,uploadSuccess : function(file, serverData) {
		try{
			var response = $.parseJSON(serverData);
		}catch(ex){
			response = serverData;
		}
		try {
			var progress = new FileProgress(file,  this.customSettings.upload_target);
			if(response.FILEID){
				if(response.request){
					$("#" + response.request.ju_element).val(response.FILEID).next(".uploading").remove();
				}
				//progress.setStatus("Thumbnail Created.");
				progress.toggleCancel(false);
			} else {
				this.uploadError(file, 1001, serverData);
			}
		} catch (ex) {
			this.debug(ex);
		}
	}
}